package com.example.websocket.controller;

import com.example.websocket.pojo.Result;
import com.example.websocket.pojo.dto.InviteDto;
import com.example.websocket.pojo.dto.LikeDto;
import com.example.websocket.pojo.dto.UserListDto;
import com.example.websocket.pojo.po.GroupPo;
import com.example.websocket.service.GroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Delete;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.example.websocket.constants.Constants.SESSION_GROUP_ID;

/**
 * @ author 韩
 * time: 2024/3/21 10:58
 */
@RestController
@RequestMapping("/group")
@Tag(name = "GroupController",description = "主要是对群聊的一些的操作")
public class GroupController {

    @Autowired
    private GroupService groupService;




    // 先写一个根据用户获取对应群聊列表的接口
    @Operation(summary = "获取一个人所属于的群聊列表")
    @GetMapping("/getOwnGroupList")
    public Result getOwnGroupList(HttpSession session) {
        String username = (String)session.getAttribute("username");
        List<GroupPo> list = groupService.getOwnGroupList(username);
        return Result.ok(list);
    }
    // 根据groupId查询群聊里的成员
    @Operation(summary = "根据groupId查询群聊里的成员")
    @GetMapping("/getMemberById")
    public Result getMemberById(HttpSession session) {
        Integer groupID = (Integer)session.getAttribute(SESSION_GROUP_ID);
        List<String> list = groupService.getMemberById(groupID);
        return Result.ok(list);
    }



    @Operation(summary = "在session中存入一个groupId")
    @GetMapping("/storeId")
    public Result storeId(HttpSession session, @RequestParam("groupId") String groupId) {
        session.setAttribute(SESSION_GROUP_ID,groupId);
        return Result.ok("存入成功");
    }
    @Operation(summary = "在session中删除一个groupId")
    @DeleteMapping("/deleteId")
    public Result deleteId(HttpSession session) {
        session.removeAttribute(SESSION_GROUP_ID);
        return Result.ok("存入失败");
    }
    @Operation(summary = "测试session中是否有groupId")
    @GetMapping("/getId")
    public Result getId(HttpSession session) {
        return Result.ok(session.getAttribute(SESSION_GROUP_ID));
    }

    @Operation(summary = "邀请一堆好友",description = "无需同意，直接进群，报错的话，也就是通过异常处理器flag为false了，前端就提示已经群成员")
    @PostMapping("/invite")
    public Result invite(@RequestBody List<InviteDto> dtos) {

        dtos.forEach(
            dto -> {
                String invitedUsername = dto.getUsername();
                Integer groupId = dto.getGroupId();
                groupService.invite(groupId, invitedUsername);
            }
        );
        return Result.ok();
    }


    @Operation(summary = "创建一个群聊",description = "创始人拥有权力删除人")
    @Parameters({@Parameter(name = "creator", description = "创始人", in = ParameterIn.COOKIE),
            @Parameter(name = "groupName", description = "群聊的名字", in = ParameterIn.DEFAULT)
    })
    @PostMapping("/createGroup")
    public Result createGroup(@RequestBody Map<String, String> requestBody,HttpSession session) {
        String creator = (String) session.getAttribute("username");

        String groupName = requestBody.get("groupName");
        groupService.createGroup(groupName,creator);
        return Result.ok();
    }

    @Operation(summary = "查询出可以邀请的朋友列表",description = "就是朋友减去群里的朋友")
    @Parameters({
            @Parameter(name = "username",description = "主要做")
    })
    @GetMapping("/getInvitedFriends")
    public Result getInvitedFriends(String username, Integer groupId) {
        List<UserListDto> invitedFriends = groupService.getInvitedFriends(username,groupId);
        return Result.ok(invitedFriends);

    }


}